﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for frmLogin.
	/// </summary>
	public class frmLogin : System.Windows.Forms.Form
	{

		private AccessData mDB=new AccessData();
		private DataSet ds =new DataSet();
        Language lan = new Language();
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		public System.Windows.Forms.TextBox txtuser;
		public System.Windows.Forms.TextBox txtpassword;
		public bool m_admin=false;


        public string c_d = "", ten_nd = "", maquyen = "", sten = "", s_makp = "", pass = "";
        private Wildgrape.Aqua.Controls.Button cmdCancel;
        private Wildgrape.Aqua.Controls.Button cmdOk;
		
		
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmLogin()
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmLogin));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.txtuser = new System.Windows.Forms.TextBox();
            this.txtpassword = new System.Windows.Forms.TextBox();
            this.cmdCancel = new Wildgrape.Aqua.Controls.Button();
            this.cmdOk = new Wildgrape.Aqua.Controls.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(12, 18);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(104, 16);
            this.label1.TabIndex = 0;
            this.label1.Text = "Tên đăng nhập :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(12, 45);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(104, 16);
            this.label2.TabIndex = 2;
            this.label2.Text = "Mật khẩu :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtuser
            // 
            this.txtuser.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtuser.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.txtuser.Location = new System.Drawing.Point(122, 15);
            this.txtuser.MaxLength = 40;
            this.txtuser.Name = "txtuser";
            this.txtuser.Size = new System.Drawing.Size(166, 23);
            this.txtuser.TabIndex = 1;
            this.txtuser.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtuser_KeyDown);
            // 
            // txtpassword
            // 
            this.txtpassword.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtpassword.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(160)))));
            this.txtpassword.Location = new System.Drawing.Point(122, 42);
            this.txtpassword.MaxLength = 30;
            this.txtpassword.Name = "txtpassword";
            this.txtpassword.PasswordChar = '*';
            this.txtpassword.Size = new System.Drawing.Size(166, 23);
            this.txtpassword.TabIndex = 3;
            this.txtpassword.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtpassword_KeyDown);
            // 
            // cmdCancel
            // 
            this.cmdCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmdCancel.Location = new System.Drawing.Point(198, 71);
            this.cmdCancel.Name = "cmdCancel";
            this.cmdCancel.SizeToLabel = false;
            this.cmdCancel.TabIndex = 18;
            this.cmdCancel.Text = "Kết thúc";
            this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
            // 
            // cmdOk
            // 
            this.cmdOk.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmdOk.Location = new System.Drawing.Point(122, 71);
            this.cmdOk.Name = "cmdOk";
            this.cmdOk.SizeToLabel = false;
            this.cmdOk.TabIndex = 17;
            this.cmdOk.Text = "Chọn";
            this.cmdOk.Click += new System.EventHandler(this.cmdOk_Click);
            // 
            // frmLogin
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(306, 117);
            this.Controls.Add(this.cmdCancel);
            this.Controls.Add(this.cmdOk);
            this.Controls.Add(this.txtpassword);
            this.Controls.Add(this.txtuser);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmLogin";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Đăng nhập";
            this.Load += new System.EventHandler(this.frmLogin_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void txtuser_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
				SendKeys.Send("{Tab}");	
			
		}

		private void txtpassword_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
            if (e.KeyCode == Keys.Enter)
				SendKeys.Send("{Tab}");	
			
		}
        
        public bool kiemtra()
        {
            try
            {

                string m_mk = "";
                if (txtuser.Text == AccessData.hrs_userid && txtpassword.Text == AccessData.hrs_pass)
                {
                    ten_nd = AccessData.s_links_userid;
                    maquyen = "00058,000-11,00502,50201";
                    sten = AccessData.hrs_userid + AccessData.hrs_pass;
                    return true;
                }
                string sql = "select * from " + mDB.user + ".pwhuman  where (trim(username))='" + txtuser.Text.Trim().ToUpper() + "'"; //order by username
                ds = mDB.get_data(sql);
                DataRow dr = mDB.getrowbyid(ds.Tables[0], "username='" + txtuser.Text.Trim().ToUpper() + "'");
                if (dr == null)
                {
                    return false;
                }
                else
                {
                    m_mk = mDB.decode(dr["pass"].ToString());
                    if (txtpassword.Text.ToUpper().Trim() != m_mk)
                    {
                        return false;
                    }
                    else
                    {
                        ten_nd = dr["username"].ToString();
                        c_d = dr["pass"].ToString();
                        maquyen = dr["_right"].ToString();
                        sten = dr["ten"].ToString();
                        m_admin = (bool)(dr["_admin"]);
                        s_makp = dr["makp"].ToString();
                        return true;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
		private void frmLogin_Load(object sender, System.EventArgs e)
		{
			txtuser.Focus();			
		}	


      

        private void cmdOk_Click(object sender, EventArgs e)
        {
            if (txtpassword.Text == "")
                txtpassword.Focus();
            else
                if (!kiemtra())
                {
                    MessageBox.Show(lan.Change_language_MessageText("Tên người dùng và mật khẩu không tìm thấy!"), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtuser.Focus();
                    txtuser.Text = "";
                    txtpassword.Text = "";
                    return;
                }
                else
                {
                    pass = txtpassword.Text.Trim();
                    this.Close();
                }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            ten_nd = "";
            Application.Exit();
        }
     
		
	}
}
